package de.moc.server;

import java.util.List;
import java.util.Vector;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import de.moc.shared.*;

public class DataAccess {

	public String saveSurvey(Survey survey, boolean active)
			throws SurveyException {
		EntityManager em = EMF.getEntityManager();

		try {
			SurveyP surveyP = PNonPConverter.SurveytoSurveyP(survey);

			surveyP.setId(UUID.uuid());
			surveyP.setActive(active);

			em.persist(surveyP);

			return surveyP.getId();
		} catch (Exception e) {
			throw new SurveyException();
		} finally {
			em.close();
		}
	}

	public boolean endActiveSurvey() {
		EntityManager em = EMF.getEntityManager();

		try {
			Query query = em
					.createQuery("SELECT s from SurveyP s where s.active = true");

			@SuppressWarnings("unchecked")
			List<SurveyP> surveyPs = (List<SurveyP>) query.getResultList();

			if (surveyPs.size() <= 0)
				return false;

			SurveyP surveyP = surveyPs.get(0);

			surveyP.setActive(false);

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			em.close();
		}
	}

	public Survey getActiveSurvey() throws SurveyException {
		EntityManager em = EMF.getEntityManager();

		try {
			Query query = em
					.createQuery("SELECT s from SurveyP s where s.active = true");

			@SuppressWarnings("unchecked")
			List<SurveyP> surveyPs = (List<SurveyP>) query.getResultList();

			if (surveyPs.size() <= 0)
				throw new SurveyException();

			SurveyP surveyP = surveyPs.get(0);

			return PNonPConverter.SurveyPtoSurvey(surveyP);
		} catch (Exception e) {
			throw new SurveyException();
		} finally {
			em.close();
		}
	}

	public Survey getSurvey(String id) throws SurveyException {
		EntityManager em = EMF.getEntityManager();

		try {
			Query query = em
					.createQuery("SELECT s from SurveyP s where s.id = '" + id
							+ "'");

			@SuppressWarnings("unchecked")
			List<SurveyP> surveyPs = (List<SurveyP>) query.getResultList();

			if (surveyPs.size() <= 0)
				throw new SurveyException();

			SurveyP surveyP = surveyPs.get(0);

			return PNonPConverter.SurveyPtoSurvey(surveyP);
		} catch (Exception e) {
			throw new SurveyException();
		} finally {
			em.close();
		}
	}

	public boolean removeSurvey(String id) {
		EntityManager em = EMF.getEntityManager();

		try {
			Query query = em
					.createQuery("SELECT s from SurveyP s where s.id = '" + id
							+ "'");

			@SuppressWarnings("unchecked")
			List<SurveyP> surveyPs = (List<SurveyP>) query.getResultList();

			if (surveyPs.size() <= 0)
				return false;

			for (int i = 0; i < surveyPs.size(); i++)
				em.remove(surveyPs.get(i));

			return true;
		} catch (Exception e) {
			return false;
		}

		finally {
			em.close();
		}
	}

	public Survey resetSurvey(String id) throws SurveyException{
		EntityManager em = EMF.getEntityManager();

		try {
			endActiveSurvey();

			Query query = em
					.createQuery("SELECT s from SurveyP s where s.id = '" + id
							+ "'");

			@SuppressWarnings("unchecked")
			List<SurveyP> surveyPs = (List<SurveyP>) query.getResultList();

			if (surveyPs.size() <= 0)
				throw new SurveyException();

			SurveyP surveyP = surveyPs.get(0);

			surveyP.resetChoiceCount();
			
			surveyP.setId(UUID.uuid());

			surveyP.setActive(true);

			return PNonPConverter.SurveyPtoSurvey(surveyP);
		} catch (Exception e) {
			throw new SurveyException();
		}

		finally {
			em.close();
		}
	}

	public Vector<Survey> getAllSurveys() throws SurveyException {
		EntityManager em = EMF.getEntityManager();

		try {
			Query query = em.createQuery("SELECT s from SurveyP s");

			@SuppressWarnings("unchecked")
			List<SurveyP> surveyPs = (List<SurveyP>) query.getResultList();

			if (surveyPs.size() <= 0)
				throw new SurveyException();

			Vector<Survey> surveys = new Vector<Survey>();

			for (int i = 0; i < surveyPs.size(); i++)
				surveys.add(PNonPConverter.SurveyPtoSurvey(surveyPs.get(i)));

			return surveys;
		} catch (Exception e) {
			throw new SurveyException();
		}

		finally {
			em.close();
		}
	}

	public boolean incChoiceOfActiveSurvey(String name) throws SurveyException {
		EntityManager em = EMF.getEntityManager();

		try {
			Query query = em
					.createQuery("SELECT s from SurveyP s where s.active = true");

			@SuppressWarnings("unchecked")
			List<SurveyP> surveyPs = (List<SurveyP>) query.getResultList();

			if (surveyPs.size() <= 0)
				throw new SurveyException();

			SurveyP surveyP = surveyPs.get(0);

			for (int i = 0; i < surveyP.getChoices().size(); i++)
				if (surveyP.getChoices().get(i).getName().equals(name)) {
					surveyP.getChoices().get(i).incCount();
					return true;
				}

			return false;
		} catch (Exception e) {
			throw new SurveyException();
		} finally {
			em.close();
		}
	}

}
